﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.ComponentModel;

namespace Qreed.Windows.ComponentModel
{
    public class PropertyBagItemCollection : CollectionBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="PropertyBagItemCollection"/> class.
        /// </summary>
        /// <param name="bag">The bag.</param>
        public PropertyBagItemCollection(PropertyBag bag)
        {
            _bag = bag;
        }

        private PropertyBag _bag;
        /// <summary>
        /// Gets or sets the bag.
        /// </summary>
        /// <value>The bag.</value>
        public PropertyBag Bag
        {
            get { return this._bag; }
            internal set 
            { 
                this._bag = value;

                foreach (PropertyBagItem item in this)
                    item.Bag = value;
            }
        }

        #region Collection stuff

        /// <summary>
        /// Adds the specified item.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns></returns>
        public int Add(PropertyBagItem item)
        {
            /*if (GetIndexOf(item.Name) != -1)
                throw (new ArgumentException("Item name \"" + item.Name + "\" already in list."));*/
            
            item.Bag = Bag;
            return List.Add(item);
        }

        /// <summary>
        /// Removes the specified name.
        /// </summary>
        /// <param name="name">The name.</param>
        public void Remove(string name)
        {
            RemoveAt(GetIndexOf(name));
        }

        /// <summary>
        /// Gets the <see cref="Qreed.Windows.ComponentModel.PropertyBagItem"/> with the specified name.
        /// </summary>
        /// <value></value>
        public PropertyBagItem this[string name]
        {
            get 
            {
                int index = GetIndexOf(name);

                if (index == -1)
                    return null;

                return (PropertyBagItem)List[index]; 
            }
        }

        /// <summary>
        /// Gets the <see cref="Qreed.Windows.ComponentModel.PropertyBagItem"/> at the specified index.
        /// </summary>
        /// <value></value>
        public PropertyBagItem this[int index]
        {
            get { return (PropertyBagItem)List[index]; }
        }

        /// <summary>
        /// Gets the index of.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public int GetIndexOf(string name)
        {
            for (int i = 0; i < Count; i++)
            {
                PropertyBagItem item = this[i];
                if (item.Name == name)
                    return i;
            }

            return -1;
        }

        internal bool Contains(PropertyBagItem item)
        {
            return List.Contains(item);
        }

        #endregion

        /// <summary>
        /// Gets the properties.
        /// </summary>
        /// <param name="attributes">The attributes.</param>
        /// <returns></returns>
        public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            PropertyDescriptorCollection ret = new PropertyDescriptorCollection(null);

            foreach (PropertyBagItem item in this)
            {
                if (item.IsBrowsable)
                {
                    ret.Add(item.GetPropertyDescriptor(attributes));

                    /*
                    if(typeof(PropertyBagExpandableItem).IsAssignableFrom(item.GetType()))
                        ret.Add(new PropertyBagExpandableItemDescriptor(item, attributes));
                    else
                        ret.Add(new PropertyBagItemDescriptor(item, attributes));
                    */
                }
            }

            return ret;
        }
    }
}
